package pers.vic.practice.module.student.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import pers.vic.boot.base.controller.BaseController;

import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.vo.Select2VO;
import pers.vic.practice.module.student.model.SClasses;
import pers.vic.practice.module.student.model.SCourse;
import pers.vic.practice.module.student.model.SStudent;
import pers.vic.practice.module.student.service.*;
import pers.vic.practice.module.student.model.SCourseSelection;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description :选课表 控制层
 * @author Vic.xu
 * @date: 2022-04-12 09:56
 */
@RestController
@RequestMapping("/student/selection")
public class SCourseSelectionController extends BaseController<SCourseSelectionService, SCourseSelection>{

    @Resource
    private SStudentService studentService;

    @Resource
    private SCourseService courseService;

    @Resource
    private SClassesService classesService;

    @Resource
    private SScoreService scoreService;

    @GetMapping("/home")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView("student/selection/home");
        List<Select2VO> classes = classesService.list(new SClasses()).stream().map(s -> new Select2VO(s.getId(), s.getName())).collect(Collectors.toList());
        modelAndView.addObject("classes", classes);
        return modelAndView;
    }

    @GetMapping("/form")
    public ModelAndView form(Integer id){
        ModelAndView modelAndView = new ModelAndView("student/selection/form");
        bindSelectInfo(modelAndView);
        if (id != null) {
            modelAndView.addObject("entity", service.findById(id));
        }
        return modelAndView;
    }

    /**
     *  前往为班级选课
     */
    @GetMapping("/saveClasses")
    public ModelAndView saveClasses(){
        ModelAndView modelAndView = new ModelAndView("student/selection/classes-form");
        bindSelectInfo(modelAndView);
        return modelAndView;
    }
    /**
     * 保存整个班级的选课
     */
    @PostMapping("/saveClasses")
    public BaseResponse<?> saveClasses(Integer classesId, Integer courseId){
        int num = service.saveClasses(classesId, courseId);
        return  BaseResponse.success(num);
    }

    /**
     * 录入分数
     * @param id 选课id
     * @param score 分数
     * @return ok
     */


    @PostMapping("/recordScore")
    public BaseResponse recordScore(Integer id, BigDecimal score){
        scoreService.recordScore(id, score);
        return BaseResponse.success();
    }


    private void bindSelectInfo(ModelAndView modelAndView){
        List<SStudent> students = studentService.list(new SStudent());
        modelAndView.addObject("students", students.stream().map(s->{
            return new Select2VO(s.getNumber(), s.getNumber()+"("+s.getName()+")");
        }).collect(Collectors.toList()));

        List<SCourse> courses = courseService.list(new SCourse());
        modelAndView.addObject("courses", courses.stream().map(s->{
            return new Select2VO(s.getId(), s.getName());
        }).collect(Collectors.toList()));
        List<Select2VO> classes = classesService.list(new SClasses()).stream().map(s -> new Select2VO(s.getId(), s.getName())).collect(Collectors.toList());
        modelAndView.addObject("classes", classes);
    }

    @PostMapping("/repeat")
    public BaseResponse<?> repeat(Integer id, Integer courseId, String number){
        boolean repeat = service.checkRepeat(id,courseId, number);
        return BaseResponse.success(!repeat);
    }

}
